package com.joyoung.common.ui;


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * 简单的BaseFragment
 *
 */
@SuppressWarnings({"WeakerAccess", "unused"})
public abstract class SimpleFragment extends Fragment implements ISimpleInit {

    protected View mContentView;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mContentView == null) {
            mContentView = inflater.inflate(layoutId(), container, false);
        }
        ViewGroup parent = (ViewGroup) mContentView.getParent();
        if (parent != null) {
            parent.removeView(mContentView);
        }
        init(savedInstanceState);
        return mContentView;
    }

    protected View getContentView() {
        return mContentView;
    }

    protected void setContentView(@NonNull View view) {
        mContentView = view;
    }

    @Nullable
    protected <T extends View> T findViewById(@IdRes int id) {
        if (mContentView == null) {
            throw new NullPointerException("ContentView is null");
        }
        return mContentView.findViewById(id);
    }

}
